<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Main extends Controller
{

    public function before()
    {
        parent::before();
        $this->template = Template::instance('admin');
        //设置公告css js
        Assets::css('message', $this->template->media_path . 'css/message.css');
        Assets::css('form', $this->template->media_path . 'css/table_form.css');
        Assets::css('reset', $this->template->media_path . 'css/reset.css');
        Assets::css('system', $this->template->media_path . 'css/system.css');
        Assets::js('jquerys', $this->template->media_path . 'js/jquery.js');
        Assets::js('message', $this->template->media_path . 'js/message.js');
        Assets::js('dialog', $this->template->media_path . 'js/lhgdialog/lhgcore.lhgdialog.min.js');
        Assets::js('validate', $this->template->media_path . 'js/jquery.validate.js');
        $this->template->assign('site_url', URL::site());
    }

    public function after()
    {
        $this->template->assign('system_css', Assets::all_css());
        $this->template->assign('system_js', Assets::all_js());
        $this->template->assign('messages', Message::get());
        Core::$environment = Core::PRODUCTION;
        if ($this->template->name != '')
        {
            $this->template->display($this->template->name);
        }
        if (Core::$environment === 'development')
        {
            echo View::factory('profiler/stats');
        }
        parent::after();
    }

    //获取普通数据的缓存
    public function get_cache_file($category_type)
    {
        $category_data = Core::config('data_cache');
        if (file_exists($category_data['file'][$category_type]))
        {
            return Cache::instance()->read($category_data['file'][$category_type]);
        }
    }

    //返回等级多维数组
    public function get_date_as_array($current_id, $category_type)
    {
        $data = $this->get_cache_file($category_type);
        Newtree::instance()->setTree($data, 'id', 'parent_id', 'name');
        $temp = Newtree::instance()->getArrayList($current_id);
        return $temp;
    }

    //获取缓存分类的option
    public function get_category_cache_option($category_type, $selected_id = 0, $current_id = 0)
    {
        $category_data = Core::config('data_cache');
        $data = $this->get_cache_file($category_type);
        Newtree::instance()->setTree($data, 'id', 'parent_id', 'name');
        if ($current_id == 0)
        {
            $current_id = $category_data['id'][$category_type];
        }
        $temp = Newtree::instance()->get_tree($current_id, $selected_id);
        Newtree::instance()->delete();
        return $temp;
    }
    
      //获取分类的checkbox
    public function get_category_cache_checkbox($category_type, $current_id = 0, $selected_id = 0, $checkbox_name)
    {
        $category_data = Core::config('data_cache');
        $data = $this->get_cache_file($category_type);
        Newtree::instance($category_type)->setTree($data, 'id', 'parent_id', 'name');
        if ($current_id == 0)
        {
            $current_id = $category_data['id'][$category_type];
        }
        $checkbox_str = "<input type='checkbox' value = \$id \$selected name='" . $checkbox_name . "' id='" . str_replace('[]', '', $checkbox_name) . "_\$id'><label for='" . str_replace('[]', '', $checkbox_name) . "_\$id'>\$name</label>";
        return Newtree::instance($category_type)->get_tree($current_id, $selected_id, $checkbox_str, 'checked');
    }

    //获取城市的缓存文件
    public function get_area_cache_file()
    {
        $area_category = $this->get_cache_file('area');
        $area = array();
        if (Arr::validate_arr($area_category))
        {
            foreach ($area_category as $key => $value)
            {
                if ($value['id'] == SYSTEM_AREA_CATEGORY)
                {
                    continue;
                }
                if ($value['parent_id'] == SYSTEM_AREA_CATEGORY)
                {
                    $area['province'][$key] = $value;
                }
                else
                {
                    $area['city'][$key] = $value;
                }
            }
        }
        return $area;
    }

    //获取分类的option(非缓存)
    public function get_category_option($data, $selected_id = 0, $current_id = 0)
    {
        if (empty($data))
        {
            return '';
        }
        Newtree::instance()->setTree($data, 'id', 'parent_id', 'name');
        $data = Newtree::instance()->get_tree($current_id, $selected_id);
        Newtree::instance()->delete();
        return $data;
    }

    //处理表单提交的数据
    public function handle_post($data)
    {
        $data_temp = array();
        if (Arr::validate_arr($data))
        {
            foreach ($data as $key => $value)
            {
                $data_temp[$key] = isset($value) ? trim($value) : '';
            }
        }
        unset($data);
        return $this->unset_post($data_temp);
    }

    public function unset_post($post, $fileds = array('submit', 'action', 'captcha', 'token'))
    {
        if (Arr::validate_arr($fileds))
        {
            foreach ($post as $key => $value)
            {
                if (in_array($key, $fileds))
                {
                    unset($post[$key]);
                }
            }
        }
        return $post;
    }

    //单张图片处理-返回图片的名称
    public function save_simple_file($dir, $filed)
    {
        //处理上传
        if (isset($_FILES))
        {
            $uploads = Upload::instance($dir)->upload($filed, 'png|jpg|gif');
            if (count($uploads) > 0)
            {
                return $uploads[0]['file_realname'];
            }
        }

        return '';
    }

    //返回图片的详细信息
    public function save_simple_detail($dir, $filed)
    {
        //处理上传
        if (isset($_FILES))
        {
            $uploads = Upload::instance($dir)->upload($filed, 'png|jpg|gif');
            if (count($uploads) > 0)
            {
                return $uploads;
            }
        }

        return '';
    }
}

